package wiki;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.simple.JSONObject;

import com.Root;

/**
 * 
 * @author mona.chandrapal
 */
public class getFileFromDB extends HttpServlet {

	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json");
		PrintWriter out = response.getWriter();
	
		Root root = new Root();
		ResultSet rs = null;
		PreparedStatement pst = null;

		InputStream sImage;
		
		
		try {

			String sql1;
			root.initConnection();
			
			sql1 = "SELECT * from attachedfile where id = 5";
			pst = root.con.prepareStatement(sql1);
			rs = pst.executeQuery();
			if (rs.next()) {
				byte[] bytearray = new byte[1024];
				int size = 0;
				sImage = rs.getBinaryStream("file");
				response.reset();
				
				boolean image = true;
				
				
				if ( image == true){
					response.setContentType("image");
					
				}else {
					response.setContentType("pdf");
					//Container content = getContentPane();
				}
				
				while ((size = sImage.read(bytearray)) != -1) {

					response.getOutputStream().write(bytearray, 0, size);
				}
				
				

				FileInputStream outstrm;
				outstrm = null;
				
				
				
				

			} else {
				out.println("<br> No photo to show ");
			}

		} catch (Exception e) {
			System.out.println("Exception downloading file: " + e.toString());
		} finally {
			try {
				pst.close();
				rs.close();
				root.closeConnection();
			} catch (Exception e) {

			}

		}

		out.println("File FROM DB");
	}

	// <editor-fold defaultstate="collapsed"
	// desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

	/**
	 * Handles the HTTP <code>GET</code> method.
	 * 
	 * @param request
	 *            servlet request
	 * @param response
	 *            servlet response
	 * @throws ServletException
	 *             if a servlet-specific error occurs
	 * @throws IOException
	 *             if an I/O error occurs
	 */
	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * Handles the HTTP <code>POST</code> method.
	 * 
	 * @param request
	 *            servlet request
	 * @param response
	 *            servlet response
	 * @throws ServletException
	 *             if a servlet-specific error occurs
	 * @throws IOException
	 *             if an I/O error occurs
	 */
	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * Returns a short description of the servlet.
	 * 
	 * @return a String containing servlet description
	 */
	@Override
	public String getServletInfo() {
		return "Short description";
	}// </editor-fold>
}
